package model

import "gorm.io/gorm"

type Like struct {
	Model
	State     bool `json:"state"`
	UserID    uint `json:"user_id,omitempty"`
	PictureID uint `json:"picture_id,omitempty"`
}

func (l Like) TableName() string {
	return "img_like"
}

func (l Like) Create(db *gorm.DB) error {
	return db.Create(&l).Error
}

func (l Like) Update(db *gorm.DB) error {
	return db.Model(&l).Select("state").Updates(&l).Error
}

func (l Like) Delete(db *gorm.DB) error {
	return db.Delete(&l).Error
}

func (l Like) Get(db *gorm.DB) (*Like, error) {
	var like Like
	err := db.Where(&l).First(&like).Error
	return &like, err
}

func (l Like) GetOrCreate(db *gorm.DB) (*Like, error) {
	var like Like
	err := db.Where(&l).FirstOrCreate(&like, l).Error
	return &like, err
}

func (l Like) List(db *gorm.DB, pageOffset int, pageSize int) ([]*Like, error) {
	var likes []*Like
	err := db.Where(&l).Offset(pageOffset).Limit(pageSize).Find(&likes).Error
	return likes, err
}

func (l Like) Count(db *gorm.DB) (int, error) {
	var count int64
	err := db.Model(&l).Where(&l).Count(&count).Error
	return int(count), err
}
